/*
 * @author: wizardc
 */

using Dou.Utils;
using UnityEngine;
using UnityEngine.UI;

namespace Dou.ScreenController
{
    /// <summary>
    /// 水平和垂直布局组件支持横竖屏切换的适配器工具类
    /// </summary>
    public static class HorizontalOrVerticalLayoutAdapterUtil
    {
        public static void CopyFormData(HorizontalOrVerticalLayoutGroup origin, HorizontalOrVerticalLayoutGroupAdapter adapter, int modeIndex, int fineTuningIndex)
        {
            if (modeIndex >= adapter.horizontalOrVerticalLayoutGroupDataList.Count)
            {
                return;
            }
            
            var data = adapter.horizontalOrVerticalLayoutGroupDataList[modeIndex];
            if (!data.isInit)
            {
                return;
            }
            
            origin.padding = data.padding;
            origin.childAlignment = data.childAlignment;
            origin.spacing = data.spacing;
            origin.childForceExpandWidth = data.childForceExpandWidth;
            origin.childForceExpandHeight = data.childForceExpandHeight;
            origin.childControlWidth = data.childControlWidth;
            origin.childControlHeight = data.childControlHeight;
            origin.childScaleWidth = data.childScaleWidth;
            origin.childScaleHeight = data.childScaleHeight;
            origin.reverseArrangement = data.reverseArrangement;

            if (fineTuningIndex >= 0 && fineTuningIndex < data.fineTuningList.Count)
            {
                var fineTuning = data.fineTuningList[fineTuningIndex];
                if (fineTuning.padding != null)
                {
                    origin.padding = fineTuning.padding;
                }
                if (fineTuning.childAlignment.HasValue)
                {
                    origin.childAlignment = fineTuning.childAlignment.Value;
                }
                if (fineTuning.spacing.HasValue)
                {
                    origin.spacing = fineTuning.spacing.Value;
                }
                if (fineTuning.childForceExpandWidth.HasValue)
                {
                    origin.childForceExpandWidth = fineTuning.childForceExpandWidth.Value;
                }
                if (fineTuning.childForceExpandHeight.HasValue)
                {
                    origin.childForceExpandHeight = fineTuning.childForceExpandHeight.Value;
                }
                if (fineTuning.childControlWidth.HasValue)
                {
                    origin.childControlWidth = fineTuning.childControlWidth.Value;
                }
                if (fineTuning.childControlHeight.HasValue)
                {
                    origin.childControlHeight = fineTuning.childControlHeight.Value;
                }
                if (fineTuning.childScaleWidth.HasValue)
                {
                    origin.childScaleWidth = fineTuning.childScaleWidth.Value;
                }
                if (fineTuning.childScaleHeight.HasValue)
                {
                    origin.childScaleHeight = fineTuning.childScaleHeight.Value;
                }
                if (fineTuning.reverseArrangement.HasValue)
                {
                    origin.reverseArrangement = fineTuning.reverseArrangement.Value;
                }
            }
        }
        
#if UNITY_EDITOR

        public static void RecodeFormLayout(HorizontalOrVerticalLayoutGroupData data, HorizontalOrVerticalLayoutGroup target)
        {
            data.padding = new RectOffset(target.padding.left, target.padding.right, target.padding.top, target.padding.bottom);
            data.childAlignment = target.childAlignment;
            data.spacing = target.spacing;
            data.childForceExpandWidth = target.childForceExpandWidth;
            data.childForceExpandHeight = target.childForceExpandHeight;
            data.childControlWidth = target.childControlWidth;
            data.childControlHeight = target.childControlHeight;
            data.childScaleWidth = target.childScaleWidth;
            data.childScaleHeight = target.childScaleHeight;
            data.reverseArrangement = target.reverseArrangement;
        }

        public static void RecodeFormLayout(HorizontalOrVerticalLayoutGroupData data, HorizontalOrVerticalLayoutGroup target, int fineTuningIndex)
        {
            if (fineTuningIndex >= 0 && fineTuningIndex < data.fineTuningList.Count)
            {
                var fineTuningData = data.fineTuningList[fineTuningIndex];
                if (!MathUtil.CheckEqual(data.padding, target.padding))
                {
                    fineTuningData.padding = new RectOffset(target.padding.left, target.padding.right, target.padding.top, target.padding.bottom);
                }
                if (data.childAlignment != target.childAlignment)
                {
                    fineTuningData.childAlignment = target.childAlignment;
                }
                if (!MathUtil.CheckEqual(data.spacing, target.spacing))
                {
                    fineTuningData.spacing = target.spacing;
                }
                if (data.childForceExpandWidth != target.childForceExpandWidth)
                {
                    fineTuningData.childForceExpandWidth = target.childForceExpandWidth;
                }
                if (data.childForceExpandHeight != target.childForceExpandHeight)
                {
                    fineTuningData.childForceExpandHeight = target.childForceExpandHeight;
                }
                if (data.childControlWidth != target.childControlWidth)
                {
                    fineTuningData.childControlWidth = target.childControlWidth;
                }
                if (data.childControlHeight != target.childControlHeight)
                {
                    fineTuningData.childControlHeight = target.childControlHeight;
                }
                if (data.childScaleWidth != target.childScaleWidth)
                {
                    fineTuningData.childScaleWidth = target.childScaleWidth;
                }
                if (data.childScaleHeight != target.childScaleHeight)
                {
                    fineTuningData.childScaleHeight = target.childScaleHeight;
                }
                if (data.reverseArrangement != target.reverseArrangement)
                {
                    fineTuningData.reverseArrangement = target.reverseArrangement;
                }
            }
        }

#endif
        
    }
}
